/* Copyright Andy Mitchell 2007. No warranties */

function ThreadActionsUI(gtdinboxPage, elParent, noCreate){
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	this._labels = this._gtdinboxPage._labels;
	
	this._eventObservers = new EventObservers(this);
	this._eventHandlers = new EventHandlers_Mutator(this);
	
	if( !noCreate ) this._create(elParent);
};

ThreadActionsUI.prototype._create = function(elParent){
	this.el = this._doc.createElement("DIV");
		this.el.className = "thread-actions";
	if( elParent ) elParent.appendChild( this.el );
	
	var elMarkRead = this._doc.createElement("BUTTON");
		elMarkRead.innerHTML = this._gtdinboxPage.stringBundle.getString("ThreadActions.MarkAsRead");	
		this._eventObservers.observe(elMarkRead, "click", this._clickMarkRead, false);
	this.el.appendChild(elMarkRead);
	
	var elArchive = this._doc.createElement("BUTTON");
		elArchive.innerHTML = this._gtdinboxPage.stringBundle.getString("ThreadActions.Archive");	
		this._eventObservers.observe(elArchive, "click", this._clickArchive, false);
	this.el.appendChild(elArchive);
	
	this.el.appendChild( this.getFinishedButton() );
		
	var elDelete = this._doc.createElement("BUTTON");
		elDelete.innerHTML = this._gtdinboxPage.stringBundle.getString("ThreadActions.Delete");	
		this._eventObservers.observe(elDelete, "click", this._clickDelete, false);
	this.el.appendChild(elDelete);
};

ThreadActionsUI.prototype.refresh = function(thread){
	this.thread = thread;
};

ThreadActionsUI.prototype.getFinishedButton = function(){
	var elFinish = this._doc.createElement("BUTTON");
		elFinish.innerHTML = this._gtdinboxPage.stringBundle.getString("ThreadActions.Finished");	
		elFinish.title = this._gtdinboxPage.stringBundle.getString("ThreadActions.Finished.tooltip");	
		this._eventObservers.observe(elFinish, "click", this._clickFinish, false);
	return elFinish;
};


ThreadActionsUI.prototype._clickMarkRead = function(event, noClickFinishEvent){
try{
	var f = GIPROTOTYPE.bind(this, function(el){
		if( el.getAttribute && el.getAttribute("act")=="1" ){
			GIPROTOTYPE.doMouseEvent(this._doc, el, "mouseup");
			if( !noClickFinishEvent ){
				setTimeout( GIPROTOTYPE.bind(this, function(){	
						this._eventHandlers.broadcast("click-finished");
					}), 500);
			}
			return true;
		}
	});
	this._labels._doMoreAction(f);
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickMarkRead\n" + e.toString());
}
};
ThreadActionsUI.prototype._clickArchive = function(event){
try{
	var el = this._gtdinboxPage.gmailDOM.element("archive-button");
	if( el ) GIPROTOTYPE.doMouseEvent(this._doc, el, "click");
	this._eventHandlers.broadcast("click-finished");
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickArchive\n" + e.toString());
}
};
ThreadActionsUI.prototype._clickDelete = function(event){
try{
	var el = this._gtdinboxPage.gmailDOM.element("delete-button");
	if( el ){
		GIPROTOTYPE.doMouseEvent(this._doc, el, "click");
		this._eventHandlers.broadcast("click-finished");
	}else{	// no delete button, could be Sent Mail?
		var f = GIPROTOTYPE.bind(this, function(el){
			if( el.getAttribute && el.getAttribute("act")=="10" ){
				GIPROTOTYPE.doMouseEvent(this._doc, el, "mouseup");
				setTimeout( GIPROTOTYPE.bind(this, function(){
						//this._eventHandlers.broadcast("click-finished");
					}), 500);
				return true;
			}
		});
		this._labels._doMoreAction(f);
	}	
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickDelete\n" + e.toString());
}
};
ThreadActionsUI.prototype._clickFinish = function(event){
try{
	// Remove all statuses; mark as read; archive
	var settingsStatuses = this._gtdinboxPage.settings.labels.types["Statuses"];
	var statusPrefix = settingsStatuses.prefix;
	var statusFinished = settingsStatuses.prefix + settingsStatuses.defaults.Finished;
	var labels = GIPROTOTYPE.clone(this.thread.labels);
	for( var i = 0; i < labels.length; i++ ){	// remove any instance of finished; incase processing after drop down label click has applied a label
		if( labels[i]==statusFinished ){
			labels.splice(i,1)
			break;
		}
	}
	var labelled = {'count':0};
	var fCallback = GIPROTOTYPE.bind(this, function(act){
		if( --labelled.count==0 ){	// move on
			this._doMarkReadAndArchive();
		}
	});
	if( labels.length > 0 ){
		for( var i = 0; i < labels.length; i++ ){
			if( labels[i].indexOf(statusPrefix)==0 ){
				labelled.count++;
				this._labels.labelThread({labelname:labels[i], 'on':false, 'threads':this.thread, 'fCallback':fCallback});
			}
			if( i==(labels.length-1) ){	// Last entry, handle Finished label
				if( this._labels.hasLabel(statusFinished) ){	// has S/Finished, so add it
					labelled.count++;
					this._labels.labelThread({labelname:statusFinished, 'on':true, 'threads':this.thread, 'fCallback':fCallback});
				}
			}
		}
	}else{
		if( this._labels.hasLabel(statusFinished) ){	// has S/Finished, so add it
			labelled.count++;
			this._labels.labelThread({labelname:statusFinished, 'on':true, 'threads':this.thread, 'fCallback':fCallback});
		}
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickFinish\n" + e.toString());
}
};
ThreadActionsUI.prototype._doMarkReadAndArchive = function(callback){
	this._clickMarkRead(null, true);
	// TODO improve by monitoring alert-box to see when mark-read is done
	setTimeout( GIPROTOTYPE.bind(this, function(){
			this._clickArchive();
			if( callback ) callback();
		}), 500 );
};




ThreadActionsUI.prototype.destroy = function(){
	this._eventHandlers.destroy();
	this._eventObservers.destroy();
};